package com.atguigu.guliedu.edu.controller;


import com.atguigu.common.utils.EduConstant;
import com.atguigu.common.utils.R;
import com.atguigu.guliedu.edu.entity.Teacher;
import com.atguigu.guliedu.edu.service.TeacherService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author java
 * @since 2021-02-01
 */

@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 分页查询
     * @param params
     * @return
     */
    @GetMapping("list")
    public R list(@RequestParam Map<String,String> params){
        Page<Teacher> list = teacherService.listTeacher(params);
        return R.ok().data("data",list);
    }

    /**
     * 修改禁用状态
     * @param teacher
     * @return
     */

    @PostMapping("status")
    public R setStatus(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return R.ok();
    }

    /**
     * 删除教师
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R removeById(@PathVariable String id){
        teacherService.removeById(id);
        return R.ok();
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    @PutMapping("update")
    public R update(@RequestBody Teacher teacher){
        if(StringUtils.isEmpty(teacher.getAvatar())){
            teacher.setAvatar(EduConstant.AVATAR);
        }
        teacherService.updateById(teacher);
        return R.ok();
    }

    /**
     * 添加教师
     * @param teacher
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody Teacher teacher){

        if(StringUtils.isEmpty(teacher.getAvatar())){
            teacher.setAvatar(EduConstant.AVATAR);
        }

        teacher.setGmtCreate(new Date());
        teacher.setGmtModified(new Date());
        teacherService.save(teacher);
        return R.ok();
    }

    /**
     * 通过id获取教师
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public R getTeacherById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("data",teacher);
    }



}

